<?php
/**
 * Server相关辅助类
 * 
 * @version $Id: IServer.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.core
 */
class IServer extends IObject {
	/**
	 * 取得命令行传入的参数
	 * 
	 * <code>
	 * 只能接受
	 *   php -f cmd.php -- -k=ko -p=po
	 * 的参数，无法分析
	 * 	 php -f cmd.php -- -k ko -p po
	 * 要实现后一种命令行的分析，请使用util.cli
	 * </code>
	 *
	 * @return array
	 */
	public static function getCmdOptions() {
		if (!isset($_SERVER["argv"])) {
			return array();
		}
		$argv = $_SERVER["argv"];
		$options = array();
		if (!empty($argv)) {
			unset($argv[0]);
			foreach ($argv as $option) {
				if (strstr($option, "=")) {
					list($optionName, $optionValue) = explode("=", $option, 2);
					$options[$optionName] = $optionValue;
				}
				else {
					$options[$option] = $option;
				}
			}
		}
		return $options;
	}
	
    /**
    * 得到当前脚本, 含查询字符串
    * 
    * 注意使用RewriteRule(如apache)可能使当前方法无法得到正确的值
    * 
    * @return string 当前脚本
    */
    public static function getRequestUri() {
        if (isset($_SERVER["REQUEST_URI"])) {
            return $_SERVER["REQUEST_URI"];
        }
        return null;
    }
    
    /**
     * 取得访问的路径
     *
     * @return string
     * @since 0.0.3
     */
    public static function getRequestPath() {
    	$uri = new IURI(self::getRequestUri());
    	return $uri->getPath();
    }

    /**
    * 得到当前页面文件名,相对于根目录
    * 
    * 注意使用RewriteRule(如apache)可能使当前方法无法得到正确的值
    * 
    * @return string 当前页面文件名
    */
    public static function getRequestPage() {
        if (isset($_SERVER["PHP_SELF"])) {
            return $_SERVER["PHP_SELF"];
        }
        if (isset($_SERVER["SCRIPT_NAME"])) {
            return $_SERVER["SCRIPT_NAME"];
        }
        return null;
    }

    /**
    * 得到当前客户端的IP
    * 
    * @return string 当前客户端的IP
    */
    public static function getIP() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        }
        elseif (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }
        elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
            $ip = getenv("REMOTE_ADDR");
        }
        elseif (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
            $ip = $_SERVER["REMOTE_ADDR"];
        }
        else {
            $ip = "0.0.0.0";
        }
        return $ip;
    }

    /**
    * 得到当前客户端的操作系统名称
    * 
    * @return string 当前客户端的操作系统名称
    */
    public static function getOS () {
        $agent = $_SERVER["HTTP_USER_AGENT"];
        $os = false;
        if (preg_match("/win/i", $agent) && strpos($agent, "95")) {
            $os = "Windows 95";
        }
        else if (preg_match("/win 9x/i", $agent) && strpos($agent, "4.90")) {
            $os = "Windows ME";
        }
        else if (preg_match("/win/i", $agent) && preg_match("/98/", $agent)) {
            $os = "Windows 98";
        }
        else if (preg_match("/win/i", $agent) && preg_match("/nt 5.1/i", $agent)) {
            $os = "Windows XP";
        }
        else if (preg_match("/win/i", $agent) && preg_match("/nt 5/i", $agent)) {
            $os = "Windows 2000";
        }
        else if (preg_match("/win/i", $agent) && preg_match("/nt/i", $agent)) {
            $os = "Windows NT";
        }
        else if (preg_match("/win/i", $agent) && preg_match("/32/", $agent)) {
            $os = "Windows 32";
        }
        else if (preg_match("/linux/i", $agent)) {
            $os = "Linux";
        }
        else if (preg_match("/unix/i", $agent)) {
            $os = "Unix";
        }
        else if (preg_match("/sun/i", $agent) && preg_match("/os/i", $agent)) {
            $os = "SunOS";
        }
        else if (preg_match("/ibm/i", $agent) && preg_match("/os/i", $agent)) {
            $os = "IBM OS/2";
        }
        else if (preg_match("/Mac/i", $agent) && preg_match("/PC/i", $agent)) {
            $os = "Macintosh";
        }
        else if (preg_match("/PowerPC/i", $agent)) {
            $os = "PowerPC";
        }
        else if (preg_match("/AIX/i", $agent)) {
            $os = "AIX";
        }
        else if (preg_match("/HPUX/i", $agent)) {
            $os = "HPUX";
        }
        else if (preg_match("/NetBSD/i", $agent)) {
            $os = "NetBSD";
        }
        else if (preg_match("/BSD/i", $agent)) {
            $os = "BSD";
        }
        else if (preg_match("/OSF1/i", $agent)) {
            $os = "OSF1";
        }
        else if (preg_match("/IRIX/i", $agent)) {
            $os = "IRIX";
        }
        else if (preg_match("/FreeBSD/i", $agent)) {
            $os = "FreeBSD";
        }
        else if (preg_match("/teleport/i", $agent)) {
            $os = "teleport";
        }
        else if (preg_match("/flashget/i", $agent)) {
            $os = "flashget";
        }
        else if (preg_match("/webzip/i", $agent)) {
            $os = "webzip";
        }
        else if (preg_match("/offline/i", $agent)) {
            $os = "offline";
        }
        else {
             $os = "Unknown";
        }
        return $os;
    }

    /**
    * 得到当前客户端所用浏览器
    * 
    * @return string 当前客户端所有浏览器
    */
    public static function getBrowser() {
        $Agent = $_SERVER["HTTP_USER_AGENT"];
        $browser = "";
        $browserver = "";
        $Browsers = array("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "MacWeb", "WebExplorer", "OmniWeb");
        for ($i = 0; $i <= 7; $i ++) {
            if(strpos($Agent, $Browsers[$i])) {
                $browser = $Browsers[$i];
            }
        }
        if (preg_match("/Mozilla/", $Agent)) {
            if (preg_match("/MSIE/", $Agent)) {
            	$args = null;
                preg_match("/MSIE (.*);/U", $Agent, $args);
                $browserver = $args[1];
                $browser = "Internet Explorer";
            }
            else if (preg_match("/Opera/", $Agent)) {
                $temp = explode(")", $Agent);
                $browserver = $temp[1];
                $temp = explode(" ", $browserver);
                $browserver = $temp[2];
                $browser = "Opera";
            }
            else {
                $temp = explode("/", $Agent);
                $browserver = $temp[1];
                $temp = explode(" ", $browserver);
                $browserver = $temp[0];
                $browser = "Netscape Navigator";
            }
        }
        if($browser != "") {
            $browseinfo = $browser . " " . $browserver;
        }
        else {
            $browseinfo = false;
        }
        return $browseinfo;
    }
    
    /**
    * 分析服务器段签名
    * 
    * @param string $uname 
    * @return array 信息集合
    */
    public static function parseSignature($uname = null) {
        $sysmap = array (
            "HP-UX" => "hpux",
            "IRIX64" => "irix",
        );
        $cpumap = array (
            "i586" => "i386",
            "i686" => "i386",
            "ppc" => "powerpc",
        );
        if ($uname === null) {
            $uname = php_uname();
        }
        $parts = split("[[:space:]]+", trim($uname));
        $n = count($parts);

        $release = $machine = $cpu = '';
        $sysname = $parts[0];
        $nodename = $parts[1];
        $cpu = $parts[$n-1];
        $extra = "";
        if ($cpu == "unknown") {
            $cpu = $parts[$n-2];
        }

        switch ($sysname) {
            case "AIX":
                $release = "{$parts[3]}.{$parts[2]}";
                break;
            case "Windows":
                switch ($parts[1]) {
                    case "95/98":
                        $release = "9x";
                        break;
                    default:
                        $release = $parts[1];
                        break;
                }
                $cpu = "i386";
                break;
            case "Linux":
                $extra = $this->_detectGlibcVersion();
                // use only the first two digits from the kernel version
                $release = ereg_replace("^([[:digit:]]+\\.[[:digit:]]+).*", "\\1", $parts[2]);
                break;
            case "Mac" :
                $sysname = "darwin";
                $nodename = $parts[2];
                $release = $parts[3];
                if ($cpu == "Macintosh") {
                    if ($parts[$n - 2] == "Power") {
                        $cpu = "powerpc";
                    }
                }
                break;
            case "Darwin" :
                if ($cpu == "Macintosh") {
                    if ($parts[$n - 2] == "Power") {
                        $cpu = "powerpc";
                    }
                }
                $release = ereg_replace("^([[:digit:]]+\\.[[:digit:]]+).*", "\\1", $parts[2]);
                break;
            default:
                $release = ereg_replace("-.*", "", $parts[2]);
                break;
        }

        if (isset($sysmap[$sysname])) {
            $sysname = $sysmap[$sysname];
        } 
        else {
            $sysname = strtolower($sysname);
        }
        if (isset($cpumap[$cpu])) {
            $cpu = $cpumap[$cpu];
        }
        return array($sysname, $release, $cpu, $extra, $nodename);
    }
}


?>